/**
 * Created by vector on 2017/11/17.
 * App入口页面
 */

import React from 'react';
import {
    ToastAndroid,
    BackAndroid,
} from 'react-native';

import { Navigator } from 'react-native-deprecated-custom-components';
import Main from './main';

var _navigator;
var lastBackPressed;
BackAndroid.addEventListener('hardwareBackPress', () => {
    if (_navigator && _navigator.getCurrentRoutes().length > 1) {
        _navigator.pop();
        return true;
    } else if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) {
        //最近2秒内按过back键，可以退出应用。
        return false;
    }
    ToastAndroid.show("再按一次退出应用", ToastAndroid.SHORT);
    lastBackPressed = Date.now();
    return true;
});

export default class App extends React.Component {
    render() {
        let defaultName = "Main";
        let defaultComponent = Main;
        return (
            <Navigator
                initialRoute={{ name: defaultName, component: defaultComponent }}
                configureScene={(route) => {
                    return Navigator.SceneConfigs.FadeAndroid;
                }}
                renderScene={(route, navigator) => {
                    let Component = route.component;
                    _navigator = navigator;
                    return <Component {...route.passProps} navigator={navigator} />
                }
                }
            />
        );
    }
}